package com.loveyukari.service;

import com.loveyukari.common.Pager;
import com.loveyukari.common.exception.InternalException;
import com.loveyukari.dao.CosmeticsDao;
import com.loveyukari.model.Cosmetics;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CosmeticsService {

    @Autowired
    private CosmeticsDao cosmeticsDao;


    public Cosmetics getCosmeticsDetail(int id) {
        Cosmetics cosmetics = cosmeticsDao.selectById(id);
        return cosmetics;
    }

    public List<Cosmetics> getCosmeticsListByPage(Pager pager,Integer status) {
        List<Cosmetics> list = cosmeticsDao.selectByPage(pager,status);
        pager.setTotal(cosmeticsDao.countAll());
        return list;
    }

    public void addCosmetics(Cosmetics cosmetics) throws InternalException {
        if (StringUtils.isBlank(cosmetics.getBrand())){
            throw new InternalException("请输入品牌");
        }
        if (StringUtils.isBlank(cosmetics.getName())){
            throw new InternalException("请输入名字");
        }
        cosmeticsDao.insert(cosmetics);
    }

    public void updateCosmetics(Cosmetics cosmetics) throws InternalException {
        cosmeticsDao.update(cosmetics);
    }

    public void updateSuccess(int id) {
        Cosmetics cosmetics = cosmeticsDao.selectById(id);
        if (cosmetics == null || cosmetics.getStatus() != 1){
            return;
        }
        cosmeticsDao.updateSuccess(id);
    }

    public void updateAccount(int id,String account){
        Cosmetics cosmetics = cosmeticsDao.selectById(id);
        if (cosmetics == null || cosmetics.getStatus() == 2){
            return;
        }
        cosmeticsDao.updateAccount(id,account);
    }

    public void close(int id){
        cosmeticsDao.updateClose(id);
    }


}
